#include <QTextStream>
#include <QFileDialog>

#include "dialogprojectsettings.h"
#include "ui_dialogprojectsettings.h"

DialogProjectSettings::DialogProjectSettings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogProjectSettings)
{


    ui->setupUi(this);
    setWindowTitle("Complete Project Settings");


    ui->comboBox_CamResolution->addItem("640x480",QVariant(480));
    ui->comboBox_CamResolution->addItem("1280x720",QVariant(720));
    ui->comboBox_CamResolution->addItem("1920x1080",QVariant(1080));

    ui->comboBox_NumCams->addItem("1",1);
    ui->comboBox_NumCams->addItem("2",2);




}

DialogProjectSettings::~DialogProjectSettings()
{
    delete ui;

    //write the event description to file
    if(!testDesc.isEmpty()){
        QFile readwrite(eventName + "/EventDescription.txt");
        readwrite.open(QIODevice::ReadOnly);
        if(readwrite.isOpen()) {
            QString str=readwrite.readAll();
            if(str.compare(testDesc)) {//if they are different
                readwrite.close();
                readwrite.open(QIODevice::WriteOnly);
                QTextStream outstream(&readwrite);
                outstream <<testDesc;
            }
            readwrite.close();
        }
        else {
            readwrite.open(QIODevice::WriteOnly);
            if(readwrite.isOpen()) {
                QTextStream outstream(&readwrite);
                outstream <<testDesc;
            }
        }
    }
}

void DialogProjectSettings::on_buttonBox_accepted()
{

}

void DialogProjectSettings::on_pushButton_ProjectFolder_clicked()
{
    //Open file dialog to set the project Directory
    QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
    projectDirectory = QFileDialog::getExistingDirectory(this,
                                                         QString("Set the Project Folder"),
                                                          projectDirectory,
                                options);
    if (!projectDirectory.isEmpty())
      ui->label_Project->setText(projectDirectory);

}

void DialogProjectSettings::setCurrentValues() {
    //Sets the current values
    if (!projectDirectory.isEmpty()) ui->label_Project->setText(projectDirectory);
    if (!eventName.isEmpty()) ui->label_Event->setText(eventName);
    if(!testDesc.isEmpty()) ui->textTestDesc->setPlainText(testDesc);
    if(camResolution) {
        int index = ui->comboBox_CamResolution->findData(camResolution);
        if ( index != -1 )  // -1 for not found
           ui->comboBox_CamResolution->setCurrentIndex(index);
    }
    if(numCams) {
        int index = ui->comboBox_NumCams->findData(numCams);
        if ( index != -1 )  // -1 for not found
           ui->comboBox_NumCams->setCurrentIndex(index);
    }

}

void DialogProjectSettings::on_pushButton_EventName_clicked()
{
    //Open file dialog to set the event Directory
    QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;

    eventName = QFileDialog::getExistingDirectory(this,
                                                  QString("Set the Event Folder"),
                                                  projectDirectory,
                                                  options);
    if (!eventName.isEmpty()) {
        ui->label_Event->setText(eventName);

        // see if description file exists
        QFile read(eventName + "/EventDescription.txt");
        read.open(QIODevice::ReadOnly);
        if(read.isOpen()) {
           QString str=read.readAll();
           ui->textTestDesc->setPlainText(str);
        }
    }
}

void DialogProjectSettings::on_textTestDesc_textChanged()
{
    testDesc=ui->textTestDesc->toPlainText();
}


void DialogProjectSettings::on_comboBox_CamResolution_currentIndexChanged(int index)
{
    camResolution = ui->comboBox_CamResolution->itemData(index).toInt();
}

void DialogProjectSettings::on_comboBox_NumCams_currentIndexChanged(int index)
{
    numCams= ui->comboBox_NumCams->itemData(index).toInt();
}
